package studio7.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import studio7.exception.DAOException;
import studio7.modelo.Sala;
import studio7.negocio.InvitadoNegocio;

/**
 * Servlet implementation class ListarSalasServlet
 */
@WebServlet("/ListarSalasServlet")
public class ListarSalasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListarSalasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	String nombresala = "";
    	String local = "";
    	int cantguitar = 0;
    	int teclado = 2;
    	int capacidad = 0;
    	int servgrabacion = 2;
    	String fecha = "";
    	String hora = "";
    	
    	//Recibo los parametros
    	if(request.getParameter("nombresala")!=null || request.getParameter("nombresala")!=""){
    		nombresala = (String)request.getParameter("nombresala").toString().trim();
    	}
    	if(request.getParameter("local")!=null || request.getParameter("local")!=""){
    		local = (String)request.getParameter("local").toString().trim();
    	}    	
    	if(request.getParameter("cantguitar")!=null && request.getParameter("cantguitar").toString().trim()!="" ){
    		cantguitar = Integer.valueOf(request.getParameter("cantguitar"));
    	}    		
    	if(request.getParameter("teclado")!=null && request.getParameter("teclado")!=""){
    		teclado = Integer.valueOf(request.getParameter("teclado"));
    	}
    	if(request.getParameter("capacidad")!=null && request.getParameter("capacidad")!=""){
    		capacidad = Integer.valueOf(request.getParameter("capacidad"));
    	}
    	if(request.getParameter("servgrabacion")!=null && request.getParameter("servgrabacion")!=""){
    		servgrabacion = Integer.valueOf(request.getParameter("servgrabacion"));
    	}
    	if(request.getParameter("fecha")!=null || request.getParameter("fecha")!=""){
    		fecha = (String)request.getParameter("fecha").toString().trim();
    	}
    	if(request.getParameter("hora")!=null || request.getParameter("hora")!=""){
    		hora = (String)request.getParameter("hora").toString().trim();
    	}
    	
		InvitadoNegocio neg = new InvitadoNegocio(); 
        try {
        	
			Collection<Sala> listaSalas = neg.listadoSalas(nombresala, local, cantguitar, teclado, capacidad, servgrabacion, fecha, hora);
			request.setAttribute("listaSalas", listaSalas);
			
			//RequestDispatcher rd = request.getRequestDispatcher("ListarSalas.jsp?p="+Math.random());
			RequestDispatcher rd = request.getRequestDispatcher("ListarSalas.jsp");
			rd.forward(request, response);
			
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
